# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
# from openpyxl import Workbook
import time

from scrapy.exporters import CsvItemExporter

from EastMoney.items import EastmoneyItem, CommentItem


class EastMoneyPipeline(object):
    def __init__(self):
        self.file_name = None
        self.file = None
        self.exporter = None
        self.comment_file_name = None
        self.comment_file = None
        self.comment_exporter = None

    def process_item(self, item, spider):
        # print(item.type)
        if item.type == 'article':
            return self.article_process_item(item, spider)
        if item.type == 'comment':
            return self.comment_process_item(item, spider)

    def close_spider(self, spider):
        # 停止exporter
        if self.exporter is not None:
            self.exporter.finish_exporting()
        if self.comment_exporter is not None:
            self.comment_exporter.finish_exporting()
        if self.file is not None:
            self.file.close()
        if self.comment_file is not None:
            self.comment_file.close()
        # 停止记录文件
        if spider.log_file is not None:
            spider.log_file.close()

    def article_process_item(self, item, spider):
        if self.file is None:
            self.file_name = "money_data.csv" if spider.code is None else "money_data_" + spider.code + ".csv"
            # 二进制读写打开文件
            self.file = open(self.file_name, 'ab+')
            # 指定编码
            self.exporter = CsvItemExporter(self.file, encoding='gb18030')
            self.exporter.start_exporting()
        # print("写入：" + item['url'])
        self.exporter.export_item(item)
        # 给spider传入文件变量
        spider.data_file = self.file

    def comment_process_item(self, item, spider):
        if self.comment_file is None:
            self.comment_file_name = "money_comment_data.csv" if spider.code is None else "money_comment_data_" + spider.code + ".csv"
            # 二进制读写打开文件
            self.comment_file = open(self.comment_file_name, 'ab+')
            # 指定编码
            self.comment_exporter = CsvItemExporter(self.comment_file, encoding='gb18030')
            self.comment_exporter.start_exporting()
        # print("写入：" + item['url'] + '的评论')
        self.comment_exporter.export_item(item)
        # 给spider传入文件变量
        spider.comment_data_file = self.comment_file
        return item
